package com.qianseit.westore.activity;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.beautymiracle.androidclient.R;
import com.qianseit.westore.BaseDoFragment;
import com.qianseit.westore.Run;

public class ExpressPickerFragment extends BaseDoFragment implements OnItemClickListener {

    private ListView mListView;

    private ArrayList<JSONObject> mSources = new ArrayList<JSONObject>();

    public ExpressPickerFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent data = mActivity.getIntent();
        try {
            String jsonStr = data.getStringExtra(Run.EXTRA_VALUE);
            JSONArray child = new JSONArray(jsonStr);
            if (child != null && child.length() > 0) {
                for (int i = 0, c = child.length(); i < c; i++)
                    mSources.add(child.getJSONObject(i));
            }
        } catch (Exception e) {
            mActivity.finish();
        }
    }

    @Override
    public void init(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
        mActionBar.setTitle(R.string.confirm_order_express);

        mListView = new ListView(mActivity);
        mListView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        mListView.setAdapter(new ExpressAdapter());
        mListView.setOnItemClickListener(this);
        rootView = mListView;
    }

    @Override
    public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
        Intent data = new Intent();
        data.putExtra(Run.EXTRA_DATA, mSources.get(pos).toString());
        mActivity.setResult(Activity.RESULT_OK, data);
        mActivity.finish();
    }

    private class ExpressAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mSources.size();
        }

        @Override
        public JSONObject getItem(int position) {
            return mSources.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mActivity.getLayoutInflater().inflate(
                        R.layout.simple_list_item2, null);
                ((TextView) convertView.findViewById(android.R.id.text1))
                        .setTextSize(18);
            }

            JSONObject data = getItem(position);
            ((TextView) convertView.findViewById(android.R.id.text1))
                    .setText(Run.buildString(data.optString("dt_name"), "(￥",
                            data.optString("money"), ")"));
            ((TextView) convertView.findViewById(android.R.id.text2))
                    .setText(Html.fromHtml(data.optString("detail")));

            return convertView;
        }
    }
}
